package com.yjf.ljr.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.yjf.ljr.R;
import com.yjf.ljr.util.ImageUtils;
import com.yjf.ljr.util.PicLoad;

public class PreviewPhotoDialog extends Dialog {
	private String path;
	private Bitmap bitmap;

	public PreviewPhotoDialog(Context context, String path) {
		super(context, R.style.dialog);
		this.path = path;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.previewphoto);
		findViewById(R.id.previewphoto_close).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		if (path.startsWith("http://")) {
			PicLoad.getImage((ImageView) findViewById(R.id.previewphoto_photo), "PreviewPhotoDialog", path);
			return;
		}

		int side = 1000;
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);
		int dWidth = options.outWidth / side;
		int dHeight = options.outHeight / side;
		if (dWidth < dHeight) {
			options.inSampleSize = dHeight;
		} else {
			options.inSampleSize = dWidth;
		}

		options.inJustDecodeBounds = false;
		bitmap = BitmapFactory.decodeFile(path, options);
//		if (Math.abs(options.outWidth - options.outHeight) < 20) {
//			((ImageView) findViewById(R.id.previewphoto_photo)).setScaleType(ScaleType.FIT_CENTER);
//		} else 
		int degree = ImageUtils.readPictureDegree(path);
		bitmap = ImageUtils.rotaingImageView(degree, bitmap);
		((ImageView) findViewById(R.id.previewphoto_photo)).setImageBitmap(bitmap);
	}

	@Override
	public void dismiss() {
		if (bitmap != null) {
			bitmap.recycle();
		}
		super.dismiss();
	}

}